﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using BaBaoFan.Modifier.Model;
using BaBaoFan.Modifier.Models;
using BaBaoFan.Modifier.Service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Caching.Memory;

namespace BaBaoFan.Modifier.Controllers
{
    public class PoolInfoController : BaseController
    {
        private readonly IPoolService _poolService;
        private readonly ICoinInfoService _coinInfoService;
        private readonly IMapper mapper;
        public PoolInfoController(IPoolService poolService, ICoinInfoService coinInfoService, IMapper mapper, IMemoryCache memoryCache) : base(memoryCache)
        {

            _poolService = poolService;
            _coinInfoService = coinInfoService;
            this.mapper = mapper;
        }
        public IActionResult List()
        {
            var model = _poolService.GetPoolInfos().OrderBy(x => x.Sort);
            return View(model);
        }
        public IActionResult Delete(int[] ids)
        {
            _poolService.Delete(ids);
            return Json("ok");
        }
        [HttpGet]
        public IActionResult Add()
        {
            var vm = new PoolInfoCreateOrUpdateViewModel
            {
                AvaliablePoolAreas = _poolService.GetPoolAreas().Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.CountryName })
            };
            return View(vm);
        }
        [HttpPost]
        public IActionResult Add(PoolInfoCreateOrUpdateViewModel vm)
        {
            if (!ModelState.IsValid)
                return View(vm);
            var model = mapper.Map<PoolInfo>(vm);
            _poolService.Add(model);
            return Redirect("/PoolInfo/List");
        }
        [HttpGet]
        public IActionResult Edit(int id)
        {
            var poolInfo = _poolService.GetPoolInfoById(id);
            var vm = mapper.Map<PoolInfoCreateOrUpdateViewModel>(poolInfo);
            vm.AvaliablePoolAreas = _poolService.GetPoolAreas().Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.CountryName });
            return View(vm);
        }
        [HttpPost]
        public IActionResult Update(PoolInfoCreateOrUpdateViewModel vm)
        {
            if (!ModelState.IsValid)
                return View(vm);
            var model = mapper.Map<PoolInfo>(vm);
            _poolService.Update(model);
            return Redirect("/PoolInfo/List");
        }
        [HttpGet]
        public IActionResult AddPoolCoin()
        {
            var model = new PoolCoinCreateModel()
            {
                AvaliableCoins = _coinInfoService.GetAllCoinInfos().Select(x => new SelectListItem { Text = x.CoinShort, Value = x.Id.ToString() }),
                AvaliablePools = _poolService.GetPoolInfos().Select(x => new SelectListItem { Text = x.PoolName, Value = x.Id.ToString() }),
            };
            return View(model);
        }

        [HttpPost]
        public IActionResult AddPoolCoin(PoolCoinCreateModel vm)
        {
            if (!ModelState.IsValid)
                return View(vm);
            var poolCoin = mapper.Map<PoolCoin>(vm);
            poolCoin.Date = DateTime.Now;
            _poolService.AddPoolCoin(poolCoin);
            return RedirectToAction(nameof(List));
        }
    }
}